package com.cnswhy.cloud.security.starter.handler;

import com.cnswhy.cloud.common.core.entity.CnswhyResponse;
import com.cnswhy.cloud.common.core.utils.CnswhyUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@Slf4j
public class CnswhyAuthExceptionEntryPoint  implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
        String requestUri = httpServletRequest.getRequestURI();
        int status = HttpServletResponse.SC_UNAUTHORIZED;
        String message = "访问令牌不合法";
        log.error("客户端访问{}请求失败: {}", requestUri, message, e);
        CnswhyUtil.makeJsonResponse(httpServletResponse, status, new CnswhyResponse().message(message));
    }
}
